package sunwin.yoms.service.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.dao.NavigationDao;
import sunwin.yoms.domain.system.NavigationDomain;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by watson on 2015/7/30.
 */
@Service
public class NavigationService {
    @Autowired
    private NavigationDao navigationDao;

    //查询
    public List<NavigationDomain> getNavigation(NavigationDomain navigationDomain) throws YomsException {
        List<NavigationDomain> navigationDomains = new ArrayList<NavigationDomain>();
        navigationDomains = navigationDao.getDomainCollection(navigationDomain);
        if (navigationDomains.size() == 0) {
            throw new YomsException("没有找到数据", 1);
        }
        return navigationDomains;
    }

    //id查询
    public NavigationDomain getNavigationById(int navigationId) {
        return navigationDao.getDomainById(navigationId);
    }

    //总数量
    public int getNavigationCount(int navigationId) {
        return navigationDao.getDomainCount();
    }

    //增加
    public void createNavigation(NavigationDomain navigationDomain) throws YomsException {
        NavigationDomain navigations = new NavigationDomain();
        navigations.setNavigationName(navigationDomain.getNavigationName());
        List<NavigationDomain> navigationDomains = navigationDao.getDomainByCondition(navigations);
        if (navigationDomains.size() > 0) {
            throw new YomsException("该导航已存在", 1);
        } else {
            if (navigationDao.addDomain(navigationDomain) != 1) {
                throw new YomsException("增加网站导航失败");
            }
        }
    }

    //删除
    public void delNavigation(int id) throws YomsException {
        NavigationDomain navigationDomain = new NavigationDomain();
        navigationDomain.setNavigationId(id);
        NavigationDomain domain = navigationDao.getDomainById(id);
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            if (navigationDao.deleteDomain(navigationDomain) != 1) {
                throw new YomsException("删除网站导航失败");
            }
        }

    }

    //更新
    public void updateNavigation(NavigationDomain navigationDomain) throws YomsException {
        NavigationDomain domain = navigationDao.getDomainById(navigationDomain.getNavigationId());
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            if (navigationDao.updateDomain(navigationDomain) != 1) {
                throw new YomsException("更新网站导航失败");
            }
        }

    }
}
